package com.example.myapp8_2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

   public  MyDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper = new MyDatabaseHelper(this, "library2.db" ,null, 2);

        dbHelper = new MyDatabaseHelper(this, "library2.db", null, 1);
        Button createDatabase = (Button) findViewById(R.id.create_database);
        createDatabase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbHelper.getWritableDatabase();
            }
        });

        Button addData = (Button) findViewById(R.id.add_data);
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();

                values.put("category_name", "经济类");
                values.put("category_code",101);
                db.insert("Category", null, values);

                values.put("category_name", "管理类");
                values.put("category_code",102);
                db.insert("Category", null, values);
            }
        });

        Button toupdate = (Button) findViewById(R.id.to_update);
        toupdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, AddBookActivity.class);
                startActivity(intent);
            }
        });

        Button getdata = (Button)  findViewById(R.id.get_data);
        getdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2 = getIntent();
                Bundle bundle = intent2.getExtras();
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values2 = new ContentValues();
                values2.put("author",bundle.getString("author"));
                values2.put("price",bundle.getString("price"));
                values2.put("pages",bundle.getString("pages"));
                values2.put("name",bundle.getString("name"));

                db.insert("book",null,values2);
                Log.i("提示信息","插入完毕");

//                Cursor cursor = db.query("Book",null,null,
//                        null,null,null,null);
//                if(cursor.moveToFirst()) {
//                    do {
////                        String name = cursor.getString(cursor.getColumnIndex("name"));
////                        String author = cursor.getString(cursor.getColumnIndex("author"));
////                        String pages = cursor.getString(cursor.getColumnIndex("pages"));
////                        String price = cursor.getString(cursor.getColumnIndex("price"));
////
//                        Log.d("MainActivity",bundle.getString("author"));
//                        Log.d("MainActivity",bundle.getString("price"));
//                        Log.d("MainActivity",bundle.getString("pages"));
//                        Log.d("MainActivity",bundle.getString("name"));
//
//
//
//
//
//
//
//
//                    } while (cursor.moveToNext());
//                }
//                cursor.close();
            }
        });


    }
}